//
//  HMMainViewController.swift
//  Weibo
//
//  Created by itteacher on 2017/7/22.
//  Copyright © 2017年 itteacher. All rights reserved.
//

import UIKit

class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //  添加子视图控制器1
//        viewControllers = [xxx, xxx]
        //  添加子视图控制器2
//        addChildViewController(xx)
        
        //  只读属性不能直接赋值
//        self.tabBar = HMTabBar()
        //  使用kvc给只读属性赋值，本质是是给只读属性的成员变量赋值
        let hmTabBar = HMTabBar()
        self.setValue(hmTabBar, forKey: "tabBar")
        
        //  设置代理
        hmTabBar.hmDelegate = self
        
        //  设置闭包
        //  需要注意循环引用
        hmTabBar.composeButtonClosure = { [unowned self] in
            //  定义的闭包
            print("我是闭包回调过来的")
            print(self)
            
            let composeView = HMComposeView()
            composeView.show(target: self)
            
            
        }
        
        addChildViewController(childController: HMHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: HMMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: HMDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: HMMineTableViewController(), title: "我的", imageName: "tabbar_profile")
    }

    //  重载添加子视图方法
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        //  如果文字颜色和图片颜色一样，可以使用该代码统一设置，但是代码应该在appdelegate里面去屑，代码越早执行越好
//        UITabBar.appearance().tintColor = UIColor.orange
        //  设置标题
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        //  合并一句代码
        childController.title = title
        //  设置图片
        //  修改图片的渲染方式
        childController.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        //  修改选中文字颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        //  设置文字大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 12)], for: .normal)
        
        //  创建导航视图控制器
        let nav = HMNavigationController(rootViewController: childController)
        
        //  添加子视图控制器
        addChildViewController(nav)
        
        
    }
    
    
    
   
}
//  MARK: -- HMTabBarDelegate
extension HMMainViewController: HMTabBarDelegate {
    func clickComposeButtonOperation() {
        print("我是代理对象调用过来的")
    }
}
